// pages/order/index/index.js
const app = getApp();
const service = require('../../../utils/service.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 总页数
    totalPage: 1,
    // 当前页码
    currentPage: 0,
    navbarList: [{
      type: 1,
      alias: '机场接送'
    }, {
        type: 2,
        alias: '巴士路线'
    }, {
        type: 3,
        alias: '专车路线'
    }, {
        type: 4,
        alias: '包车路线'
    }],
    navbarTapIndex: 0,
    // 列表
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    options.pageName && wx.setNavigationBarTitle({
      title: options.pageName,
    });
  },

  // 加载订单类型列表
  loadTypeList() {
    var that = this;
    service.getConnect(service.api.getOrderTypeList).then(function (res) {
      that.setData({
        navbarList: res.data
      })
      that.loadData();
    })
  },

  // 加载列表
  loadData() {
    var that = this;
    if (that.data.currentPage < that.data.totalPage) {
      var params = {
        type: this.data.navbarList[this.data.navbarTapIndex]['type'],
        page: this.data.currentPage + 1,
      }
      service.getConnect(service.api.getOrderList, params).then(function(res) {
        console.log(res);
        let temp = Object.assign({}, res.data);
        temp.list = that.data.list.concat(temp.list);
        that.setData(temp);
        wx.hideLoading();
      })
    }
  },

  // 菜单选择
  showActionSheet() {
    var that = this;
    var temp = [];
    for (var i = 0; i < that.data.navbarList.length; i++) {
      temp.push(that.data.navbarList[i]['alias']);
    }
    wx.showActionSheet({
      itemList: temp,
      success(res) {
        that.setData({
          navbarTapIndex: res.tapIndex,
          totalPage: 1,
          currentPage: 0,
          list: []
        })
        that.loadData();
      }
    })
  },

  // 跳转订单详情
  bintToOrderDetails(e) {
    let id = e.currentTarget.dataset.id;
    let status = e.currentTarget.dataset.status
    wx.navigateTo({
      url: '../details/details?orderSn=' + id + '&status=' + status,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      totalPage: 1,
      currentPage: 0,
      list: []
    })
    this.loadTypeList();
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2,
        selectedColor: app.globalData.config['selectedColor'],
        list: app.globalData.config.tarbarList
      })
    }
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      totalPage: 1,
      currentPage: 0,
      list: []
    })
    this.loadTypeList();
    app.utils.initWxappStyle(app, this);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.loadData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    let wxappStyle = wx.getStorageSync('wxappStyle');
    return {
      title: app.globalData['wxappStyle']['wxappTitle'] || wxappStyle['wxappTitle'],
      path: '/pages/index/index'
    }
  }
})